//
//  ContactController.m
//  MClient
//
//  Created by by zhouyi on 11-6-7.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ContactController.h"
#import "MServerDAO.h"

#import "MCTableCell.h"
#import "ContactListController.h"

@interface ContactController (Private)
- (void) initGridMenus;
- (void) initTimer;
@end

@implementation ContactController

#pragma mark ---- UITableView方法 ----

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.menus count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

	NSString *identifier = [NSString stringWithFormat:@"MenuTableCell-%d-%d", indexPath.section, indexPath.row];
	MenuCell *cell = (MenuCell*)[super tableCell:tableView withClass:[MenuCell class] withIdentifier:identifier];
	cell.backgroundColor = [UIColor colorWithPatternImage:self.userDefaults.skin_bgMenuCellImg];

	NSMutableDictionary *menu = [menusCopy objectAtIndex:indexPath.row];
	NSString *imageFile = [menu objectForKey:@"icon"];
	if (imageFile == nil || [imageFile length] == 0) {
		imageFile = @"0.png";
	}
	if ([menu objectForKey:@"label"]) {
		[cell showCell:[@"   " stringByAppendingString:[menu objectForKey:@"label"]]
			  withIcon:imageFile
				  menu:menu];
	}	
	return cell;
}


- (void)onMenuPressed:(int)index
{
	@try {
		NSDictionary *menu = [self.menus objectAtIndex:index];
		if (contactId) {
			[contactId release]; contactId = nil;
		}
		contactId = [[menu objectForKey:@"id"] copy];
		[[MServerDAO sharedInstance] downloadContact:[menu objectForKey:@"id"]
												page:-1
											delegate:self];
	}
	@catch (NSException * e) {
		[[MCErrorManagement instance] doError:@"99" 
								returnMessage:[NSString stringWithFormat:@"main: Caught %@: %@", [e name], [e reason]]];		
	}	
}


- (void)getDataFromServerSuccess: (NSMutableDictionary *)returnValue {
	ContactListController *contactListController = [[ContactListController alloc] initWithStyle:UITableViewStylePlain];
	contactListController.remoteContacts = [returnValue objectForKey:@"rl"];
	contactListController.contactId = contactId;
	[self.navigationController pushViewController:contactListController animated:YES];
	[contactListController release];
}

- (void)dealloc {
	[contactId release];
	[super dealloc];
}


@end
